#include "ActivityTimer.h"
#include "Logger.h"

CActivityTimer::~CActivityTimer()
{
	Cancel();
}

CActivityTimer* CActivityTimer::NewL(MActivityNotifier& aNotifier)
{
	CActivityTimer* timer = new (ELeave) CActivityTimer(aNotifier);
	timer->ConstructL();
	return timer;
}

CActivityTimer::CActivityTimer(MActivityNotifier& aNotifier):
CTimer(0), iNotifier(aNotifier)
{
}

void CActivityTimer::ConstructL()
{
	CActiveScheduler::Add(this);
	CTimer::ConstructL();
}

void CActivityTimer::WaitInactivity(TInt aSeconds)
{
LOG_FORMAT(_L8("Waitinactivity %d sec."), aSeconds);
	if( aSeconds == 0)
		return ;

	iSeconds = aSeconds;
	Cancel();
	Inactivity(iSeconds);
}

void CActivityTimer::RunL()
{
	TInt status = iStatus.Int();
	TInt inactivity = User::InactivityTime().Int();

	LOG_FORMAT(_L8("status %d, seconds %d, inactivity %d"), status, iSeconds, inactivity);
	
	if (iStatus == KErrNone && inactivity + 1 >= iSeconds)
	{
		iNotifier.NotifyInactivityL();
	}
	else
	{//error
		WaitInactivity(iSeconds);
	}
}
